/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entity.Users;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import util.AppHibernateUtil;
import util.Encrypt;

/**
 *
 * @author Programer
 */
public class UsersDAO {

    /////////////////////////////////////////////////////////////////
    ////Init Sigleton pattern
    private static UsersDAO Instance;
    private UsersDAO() {
    }
    public static UsersDAO getInstance() {
        if(Instance == null) {
            Instance = new UsersDAO();
        }
        return Instance;
    }
    /////////////////////////////////////////////////////////////////
    
    public static Users getUserInfo(String username) {        
        Users us = null;
        Session session = AppHibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        
        String hql = "from Users where username=:name";
        Query query = session.createQuery(hql);
        query.setParameter("name", username);
        
        if (query.list().size() > 0) {
            us = (Users) query.list().get(0);
        }
        
        return us;
    }
    
    public static boolean insertUsers(Users us) {
        us.setPassword(util.Encrypt.HasMD5(us.getPassword()));
        boolean success = false;
        Session session = AppHibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.save(us);
            session.getTransaction().commit();
            success = true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return success;
        } finally {
            session.close();
        }
        return success;
    }
    
    public static boolean checkExistedEmail(String email) {
        boolean kq = false;
        Session session = null;

        try {
            session = AppHibernateUtil.getSessionFactory().openSession();

            String hql = "FROM Users WHERE Email=:email";
            Query query = session.createQuery(hql);
            query.setParameter("email", email);

            List list = query.list();

            if (list.size() > 0) {
                kq = true;
            } else {
                kq = false;
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        } finally {
            if (session.isOpen()) {
                session.close();
            }
        }

        return kq;
    }
    
    public static boolean deleteUser(String username) {
        boolean kq = false;
        Session session = null;

        try {
            session = AppHibernateUtil.getSessionFactory().openSession();

            String hql = "DELETE FROM Users WHERE username=:usname";
            Query query = session.createQuery(hql);
            query.setParameter("usname", username);

            int amount = query.executeUpdate();
            
            if (amount == 1) {
                kq = true;
            } else {
                kq = false;
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        } finally {
            if (session.isOpen()) {
                session.close();
            }
        }

        return kq;
    }
    
    public static boolean checkExistedUsername(String username) {
        boolean kq = false;
        Session session = null;

        try {
            session = AppHibernateUtil.getSessionFactory().openSession();

            String hql = "FROM Users WHERE Username=:username";
            Query query = session.createQuery(hql);
            query.setParameter("username", username);

            List list = query.list();

            if (list.size() > 0) {
                kq = true;
            } else {
                kq = false;
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        } finally {
            if (session.isOpen()) {
                session.close();
            }
        }

        return kq;
    }
    
    public static Users logIn(String name, String pass) {
        Users result = new Users();
        pass = util.Encrypt.HasMD5(pass);
        Session session = null;

        try {
            session = AppHibernateUtil.getSessionFactory().openSession();
                        
            String hql = "FROM Users WHERE username=:name AND password=:pass";
            Query query = session.createQuery(hql);
            
            query.setParameter("name", name);
            query.setParameter("pass", pass);
            
            List list = query.list();
            
            if (list.size() > 0){
                result = (Users)list.get(0);
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        } finally {
            if (session.isOpen()) {
                session.close();
            }
        }

        return result;
    }
    
    public static ArrayList<Users> getUserList() {
        ArrayList<Users> result = new ArrayList<Users> ();
        Session session = null;

        try {
            session = AppHibernateUtil.getSessionFactory().openSession();
                        
            String hql = "FROM Users";
            Query query = session.createQuery(hql);
            
            List<Users> list = query.list();
            
            result = (ArrayList<Users>)list;
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        } finally {
            if (session.isOpen()) {
                session.close();
            }
        }

        return result;
    }
}
